/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.ContactDao;
import com.taobao.snowsnow.hibernate.pojo.Contact;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ContactDaoImpl extends BaseDaoImpl implements ContactDao {
	
	public Contact findById(int id) throws Exception {
		return (Contact) getHibernateTemplate().get(Contact.class, id);
	}
	
	public Contact findByName(String name) throws Exception {
		List<Contact> list = super.findByProperty(Contact.class, "name", name);
		if(list.size() !=0)
			return (Contact) list.get(0);
		return null;
	}

	public List<Contact> findAll() throws Exception {
		return getHibernateTemplate().find("from Contact");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Contact.class);
	}

	public List<Contact> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Contact.class, page, limit);
	}

	@Override
	public Contact getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Contact as o where o.id = (select min(id) from Contact)");
		List<Contact> list = query.list();
		if(list.size() !=0)
			return (Contact) list.get(0);
		return null;
	}

	@Override
	public Contact getLastObject() throws Exception {
		Query query = getSession().createQuery("from Contact as o where o.id = (select max(id) from Contact)");
		List<Contact> list = query.list();
		if(list.size() !=0)
			return (Contact) list.get(0);
		return null;
	}
}
